package com.ywd.materials.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ywd.materials.admin.common.Common;
import com.ywd.materials.admin.dto.query.FirstPageOneOneQuery;
import com.ywd.materials.admin.mapper.FirstPageMapper;
import com.ywd.materials.admin.utils.TreeWarehouseUtil;
import com.ywd.materials.admin.vo.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * YWD
 * 2025/11/13
 */
@AllArgsConstructor
@Service
public class FirstPageServiceImpl {

    private FirstPageMapper firstPageMapper;

    public List<Map<String, String>> getWarehouseGradeList() {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("text", "全部");
        map.put("value", "-1");
        list.add(map);
        Map<String, String> map1 = new HashMap<>();
        map1.put("text", "市本级");
        map1.put("value", "0");
        list.add(map1);
        Map<String, String> map2 = new HashMap<>();
        map2.put("text", "区县级");
        map2.put("value", "1");
        list.add(map2);
        Map<String, String> map3 = new HashMap<>();
        map3.put("text", "乡镇级");
        map3.put("value", "2");
        list.add(map3);
        Map<String, String> map4 = new HashMap<>();
        map4.put("text", "村点级");
        map4.put("value", "3");
        list.add(map4);
        return list;
    }

    public List<MaterialsWarehouseTreePartVo> getWarehouseTree(FirstPageOneOneQuery query) {
        List<MaterialsWarehouseTreePartVo> list = firstPageMapper.getWarehouseTree(query);
        return TreeWarehouseUtil.buildWarehouseTreeOne(list);
    }

    public FirstPageOneVo getOneStatistics(String areaName, String gradeValue, Long warehouseId) {
        List<Long> warehouseIds = getWarehouseIdsOne(areaName, gradeValue, warehouseId);

        FirstPageOneVo vo = new FirstPageOneVo();
        //物资总量
        Integer goodsCount = firstPageMapper.getGoodsCount(warehouseIds);
        vo.setGoodsCount(goodsCount);
        //物资分类占比列表
        List<FirstPageOnePartVo> list = new ArrayList<>();
        List<String> typeList = firstPageMapper.getGoodsType();
        List<Long> finalWarehouseIds = warehouseIds;
        typeList.forEach(item -> {
            FirstPageOnePartVo partVo = new FirstPageOnePartVo();
            partVo.setGoodsType(item);
            Integer ratio = firstPageMapper.getRatio(finalWarehouseIds, item);
            if (!goodsCount.equals(0)) {
                BigDecimal ratio1 = BigDecimal.valueOf(ratio);
                BigDecimal ratio2 = BigDecimal.valueOf(goodsCount);
                BigDecimal divide = ratio1.divide(ratio2, 4, RoundingMode.HALF_UP);
                partVo.setRatio(divide);
            } else {
                partVo.setRatio(BigDecimal.valueOf(0.0000));
            }
            list.add(partVo);
        });
        vo.setList(list);
        return vo;
    }


    public List<FirstPageTwoVo> getTwoStatistics(String areaName, String gradeValue, Long warehouseId) {
        List<Long> warehouseIds = getWarehouseIdsOne(areaName, gradeValue, warehouseId);
        FirstPageTwoTwoVo twoStatistics = firstPageMapper.getTwoStatistics(warehouseIds);
        return List.of("市本级", "区县级", "乡镇级", "村点级").stream()
                .map(item -> {
                    FirstPageTwoVo vo = new FirstPageTwoVo();
                    vo.setGradle(item);
                    vo.setCount(switch (item) {
                        case "市本级" -> twoStatistics.getOneGrade();
                        case "区县级" -> twoStatistics.getTwoGrade();
                        case "乡镇级" -> twoStatistics.getThreeGrade();
                        default -> twoStatistics.getFourGrade();
                    });
                    return vo;
                })
                .sorted(Comparator.comparing(FirstPageTwoVo::getCount).reversed())
                .collect(Collectors.toList());
    }

    public List<FirstPageThreeVo> getThreeStatistics(String areaName, String gradeValue, Long warehouseId) {
        List<Long> warehouseIds = getWarehouseIdsOne(areaName, gradeValue, warehouseId);

        return firstPageMapper.getThreeStatistics(warehouseIds);
    }

    public List<FirstPageFourVo> getFourStatistics(String areaName, String gradeValue, Long warehouseId) {
        List<Long> warehouseIds = getWarehouseIdsOne(areaName, gradeValue, warehouseId);

        //统计前七个月的数据
        return firstPageMapper.getFourStatistics(warehouseIds);
    }

    public List<FirstPageFiveVo> getFiveStatistics(String areaName, String gradeValue, Long warehouseId) {
        List<Long> warehouseIds = getWarehouseIdsOne(areaName, gradeValue, warehouseId);
        return firstPageMapper.getFiveStatistics(warehouseIds);
    }

    public FirstPageSixVo getSixStatistics(String areaName, String gradeValue, Long warehouseId, Boolean openIn) {
        List<Long> warehouseIds = getWarehouseIdsOne(areaName, gradeValue, warehouseId);
        FirstPageSixVo sixStatistics = firstPageMapper.getSixStatistics(warehouseIds);
        if (openIn) {
            sixStatistics.setSelfCount(sixStatistics.getSelfCount() + sixStatistics.getWaitSelfCount());
            sixStatistics.setPreCount(sixStatistics.getPreCount() + sixStatistics.getWaitPreCount());
        }
        return sixStatistics;
    }

    //查询下级仓库
    public List<Long> getWarehouseIds(Long warehouseId) {
        return firstPageMapper.getWarehouseIds(warehouseId);
    }

    public List<Long> getWarehouseIdsOne(String areaName, String gradeValue, Long warehouseId) {
        List<Long> warehouseIds = new ArrayList<>();
        Map<String, Long> areaMap = Map.of(
                "雨城区",4L,
                "名山区",5L,
                "天全县",6L,
                "芦山县",7L,
                 "宝兴县",8L,
                "荥经县",9L,
                 "汉源县",10L,
                "石棉县",11L
        );
        if(StringUtils.isNotBlank(areaName)){
            Long areaId = areaMap.get(areaName);
            List<Long> list = getWarehouseIds(areaId);
            if(CollectionUtil.isNotEmpty(list)){
                warehouseIds = list;
            }
        }else if(StringUtils.isNotBlank(gradeValue)){
            List<Long> list = firstPageMapper.getWarehouseIdsOne(gradeValue);
            if(CollectionUtil.isNotEmpty(list)){
                warehouseIds = list;
            }
        }else if(ObjectUtil.isNotEmpty(warehouseId)) {
            warehouseIds.add(warehouseId);
        }
        return warehouseIds;
    }


    public List<FirstPageSevenVo> getSevenStatistics() {
        List<Common> warehouseIds = firstPageMapper.getSevenStatistics();
        List<FirstPageSevenVo> list = new ArrayList<>();
        Map<Long, String> areaMap = Map.of(
                4L, "雨城区",
                5L, "名山区",
                6L, "天全县",
                7L, "芦山县",
                8L, "宝兴县",
                9L, "荥经县",
                10L, "汉源县",
                11L, "石棉县"
        );
        for (Common it : warehouseIds) {
            FirstPageSevenVo vo = new FirstPageSevenVo();
            List<Long> list1 = getWarehouseIds(it.getId());
            int size = list1.size();
            // 从 Map 中获取区域名称
            String areaName = areaMap.get(it.getId());
            if (areaName == null) {
                areaName = "未知区域";
            }
            vo.setAreaName(areaName);
            vo.setWarehouseCount(size);
            list.add(vo);
        }
        return list;
    }


}
